package com.linkme2night.model;

import java.text.DateFormat;
//import java.text.SimpleDateFormat;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public final class VenueDAO {


    Document doc;
    NodeList nodeList;
    NodeList nameList;
    NodeList cidList;
    DateFormat sdf;
    
    public VenueDAO(){
        XmlCon XmlCon = new XmlCon();
        XmlCon.LoadFile();
        doc = XmlCon.getDoc();
        nodeList = doc.getElementsByTagName("Venue");
        nameList = null;
        //sdf = new SimpleDateFormat("HH:mm");

    }

    public int[] getVenuesIDs(){
        int output[] = null;
        try{
            output = new int[nodeList.getLength()]; 
            for (int i = 0; i < nodeList.getLength(); i++) {

                Node node = nodeList.item(i);

                Element fstElmnt = (Element) node;
                cidList = fstElmnt.getElementsByTagName("VenueId");
                Element cidElement = (Element) cidList.item(0);
                cidList = cidElement.getChildNodes();

                output[i] = Integer.parseInt("" + cidList.item(0).getNodeValue());

            }
        }catch(Exception e){System.out.println(e.getMessage());}
        return output;
    }

    public double getLatitude (int venueId)
    {
        double output = 0;
        for (int i = 0; i < nodeList.getLength(); i++) {

            Node node = nodeList.item(i);

            Element fstElmnt = (Element) node;
            cidList = fstElmnt.getElementsByTagName("VenueId");
            Element cidElement = (Element) cidList.item(0);
            cidList = cidElement.getChildNodes();

            System.out.println("" + cidList.item(0).getNodeValue());
            if (cidList.item(0).getNodeValue().equals("" + venueId)) {
                nameList = fstElmnt.getElementsByTagName("Latitude");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();
            try{
                    output = new Double((((Node) nameList.item(0)).getNodeValue()));
                }catch(Exception e){System.out.println(e.getMessage());}
            }
        }
  
        return output;
    }
    
    public double getLongitude (int venueId)
    {
        double output = 0;
        for (int i = 0; i < nodeList.getLength(); i++) {
            
            Node node = nodeList.item(i);
            Element fstElmnt = (Element) node;
            cidList = fstElmnt.getElementsByTagName("VenueId");
            Element cidElement = (Element) cidList.item(0);
            cidList = cidElement.getChildNodes();

            System.out.println("" + cidList.item(0).getNodeValue());
            if (cidList.item(0).getNodeValue().equals("" + venueId)) {
                nameList = fstElmnt.getElementsByTagName("Longitude");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();
                try{
                    output = new Double((((Node) nameList.item(0)).getNodeValue()));
                }catch(Exception e){System.out.println(e.getMessage());}
            }
        }
  
        return output;
    }

    public float getOverallRanking(int venueId) {

        float output = 0;
        for (int i = 0; i < nodeList.getLength(); i++) {

            Node node = nodeList.item(i);

            Element fstElmnt = (Element) node;
            cidList = fstElmnt.getElementsByTagName("VenueId");
            Element cidElement = (Element) cidList.item(0);
            cidList = cidElement.getChildNodes();

            System.out.println("" + cidList.item(0).getNodeValue());
            if (cidList.item(0).getNodeValue().equals("" + venueId)) {
                nameList = fstElmnt.getElementsByTagName("OverallRank");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();
                try{
                    output = new Float((((Node) nameList.item(0)).getNodeValue()));
                }catch(Exception e){System.out.println(e.getMessage());}
            }
        }
  
        return output;
    }

    public String getDescription(int venueId) {

    	String output = null;
        for (int i = 0; i < nodeList.getLength(); i++) {

            Node node = nodeList.item(i);

            Element fstElmnt = (Element) node;
            cidList = fstElmnt.getElementsByTagName("VenueId");
            Element cidElement = (Element) cidList.item(0);
            cidList = cidElement.getChildNodes();

            System.out.println("" + cidList.item(0).getNodeValue());
            if (cidList.item(0).getNodeValue().equals("" + venueId)) {
                nameList = fstElmnt.getElementsByTagName("Description");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();

                try{
                    output = new String((((Node) nameList.item(0)).getNodeValue()));
                }catch(Exception e){System.out.println(e.getMessage());}
            }
        }
  
        return output;
    }

    public String getOpeningHours(int venueId) {

        String time = null;

        for (int i = 0; i < nodeList.getLength(); i++) {

            Node node = nodeList.item(i);


            Element fstElmnt = (Element) node;
            cidList = fstElmnt.getElementsByTagName("VenueId");
            Element cidElement = (Element) cidList.item(0);
            cidList = cidElement.getChildNodes();

            System.out.println("" + cidList.item(0).getNodeValue());
            if (cidList.item(0).getNodeValue().equals("" + venueId)) {
                nameList = fstElmnt.getElementsByTagName("OpeningHours");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();
                try {
                    time = (String) "" + ((Node) nameList.item(0)).getNodeValue();
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
            }

        }

        return time;
    }

    public String getClosingHours(int venueId) {
        String time = null;
        for (int i = 0; i < nodeList.getLength(); i++) {

            Node node = nodeList.item(i);

            Element fstElmnt = (Element) node;
            cidList = fstElmnt.getElementsByTagName("VenueId");
            Element cidElement = (Element) cidList.item(0);
            cidList = cidElement.getChildNodes();

            System.out.println("" + cidList.item(0).getNodeValue());
            if (cidList.item(0).getNodeValue().equals("" + venueId)) {
                nameList = fstElmnt.getElementsByTagName("ClosingHours");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();

                try {
                    time = (String) "" + ((Node) nameList.item(0)).getNodeValue();
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
            }

        }
        
        return time;
    }

    public String getAddress(int venueId) {
    	
    	String output = null;
        for (int i = 0; i < nodeList.getLength(); i++) {

            Node node = nodeList.item(i);

            Element fstElmnt = (Element) node;
            cidList = fstElmnt.getElementsByTagName("VenueId");
            Element cidElement = (Element) cidList.item(0);
            cidList = cidElement.getChildNodes();

            System.out.println("" + cidList.item(0).getNodeValue());
            if (cidList.item(0).getNodeValue().equals("" + venueId)) {
                nameList = fstElmnt.getElementsByTagName("Address");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();

                try{
                    output = new String((((Node) nameList.item(0)).getNodeValue()));
                }catch(Exception e){System.out.println(e.getMessage());}
            }
        }
  
        return output;
    }

    public String getName(int venueId) {
    	String output = null;
        for (int i = 0; i < nodeList.getLength(); i++) {

            Node node = nodeList.item(i);

            Element fstElmnt = (Element) node;
            cidList = fstElmnt.getElementsByTagName("VenueId");
            Element cidElement = (Element) cidList.item(0);
            cidList = cidElement.getChildNodes();

            System.out.println("" + cidList.item(0).getNodeValue());
            if (cidList.item(0).getNodeValue().equals("" + venueId)) {
                nameList = fstElmnt.getElementsByTagName("Name");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();

                try{
                    output = new String((((Node) nameList.item(0)).getNodeValue()));
                }catch(Exception e){System.out.println(e.getMessage());}
            }
        }
  
        return output;
    }
}